<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


/**
 * App\Models\Order
 *
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\OrderAction[] $actions
 * @property-read \App\Models\User $buyer
 * @property-read \App\Models\OrderClosed $closed
 * @property-read \App\Models\OrderItem $item
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\OrderItem[] $items
 * @property-read \App\Models\OrderRefund $refund
 * @property-read \App\Models\User $seller
 * @property-read \App\Models\OrderShipping $shipping
 * @property-read \App\Models\Shop $shop
 * @property-read \App\Models\Trade $trade
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order status($status = 1)
 * @mixin \Eloquent
 * @property int $order_id
 * @property int $order_type
 * @property int $buyer_uid 买家ID
 * @property string|null $buyer_name 买家账号
 * @property string|null $buyer_message 买家留言
 * @property int $seller_uid 卖家ID
 * @property string|null $seller_name 卖家账号
 * @property int $shop_id 店铺ID
 * @property string|null $shop_name 店铺名称
 * @property string $order_no 订单编号
 * @property float $order_fee 订单费用
 * @property float $shipping_fee 运费
 * @property float $total_fee 总费用
 * @property int $pay_type 付款方式，1=在线支付，2=货到付款
 * @property int $pay_status 支付状态，1=已支付，0=未支付
 * @property string $pay_at 付款时间
 * @property int $shipping_type 配送方式，1=快递，2=物流，3=无需物流
 * @property int $shipping_status 发货状态，0=未发货，1=已发货
 * @property string $shipping_at 发货时间
 * @property int $receive_status 收货状态，0=未收货，1=已收货
 * @property string $receive_at
 * @property int $buyer_rate 评价状态，0=未评价，1=已评价
 * @property int $seller_rate
 * @property int $refund_status 退款状态，0=无退款，1=退款中，2=退款完成
 * @property string $refund_at
 * @property string $created_at 创建时间
 * @property string $updated_at
 * @property string $completed_at 成交时间
 * @property int $commited 已提交需求
 * @property int $accepted 已受理
 * @property string $closed_at
 * @property int $buyer_deleted 已删除
 * @property int $seller_deleted
 * @property string $trade_no 交易流水号
 * @property int $trade_status 订单状态,1=未付款,2=已付款,3=已发货,4=交易完成,5=退货中,6=交易关闭
 * @property-read \App\Models\OrderClosed $closeReason
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereAccepted($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereBuyerDeleted($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereBuyerMessage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereBuyerName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereBuyerRate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereBuyerUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereClosed($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereClosedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereCommited($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereCompletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereOrderFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereOrderNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereOrderType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order wherePayAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order wherePayStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order wherePayType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereReceiveAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereReceiveStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereRefundAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereRefundStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereSellerDeleted($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereSellerName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereSellerRate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereSellerUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereShippingAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereShippingFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereShippingStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereShippingType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereShopId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereShopName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereTotalFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereTradeNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereTradeStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order whereUpdatedAt($value)
 * @property int $pay_underline 订单状态,1=未付款,2=已付款,3=已发货,4=交易完成,5=退货中,6=交易关闭
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order wherePayUnderline($value)
 * @property-read \App\Models\OrderInvoice $invoice
 */
class Order extends Model
{
    protected $table = 'order';
    protected $primaryKey = 'order_id';
    protected $guarded = ['order_d'];

    protected $observables = ['created', 'updated'];

    public $timestamps = false;

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::creating(function ($model){
            $model->created_at = time();
            $model->updated_at = time();
        });

        static::updating(function ($model){
            $model->updated_at = time();
        });

        static::deleted(function (Order $order){
            $order->items()->delete();
            $order->trade()->delete();
            $order->actions()->delete();
            $order->closeReason()->delete();
            $order->refund()->delete();
            $order->shipping()->delete();
        });
    }

    /**
     * 更改为付款状态
     */
    public function changeToPaid()
    {
        $this->update([
            'trade_status'=>2,
            'pay_status'=>1,
            'pay_at'=>time(),
            'updated_at'=>time()
        ]);

        if ($trade = $this->trade)
        {
            $trade->update([
                'trade_status'=>2,
                'pay_status'=>1,
                'pay_at'=>time(),
                'updated_at'=>time()
            ]);
        }

    }

    /**
     * 更改为发货状态
     */
    public function changeToSend()
    {
        $this->update([
            'trade_status'=>3,
            'shipping_status'=>1,
            'shipping_at'=>time(),
            'updated_at'=>time()
        ]);

        if ($trade = $this->trade)
        {
            $trade->update([
                'trade_status'=>3,
                'updated_at'=>time()
            ]);
        }
    }

    /**
     * 更改为收货状态
     */
    public function changeToReceived()
    {
        $this->update([
            'trade_status'=>4,
            'receive_status'=>1,
            'receive_at'=>time(),
            'updated_at'=>time()
        ]);

        if ($trade = $this->trade)
        {
            $trade->update([
                'trade_status'=>4,
                'updated_at'=>time()
            ]);
        }
    }

    /**
     * 更改为退款中
     */
    public function changeToRefunding()
    {
        $this->update([
            'trade_status'=>5,
            'refund_status'=>3,
            'refund_at'=>time(),
            'updated_at'=>time()
        ]);

        if ($trade = $this->trade)
        {
            $trade->update([
                'trade_status'=>5,
                'updated_at'=>time()
            ]);
        }
    }

    /**
     * 更改为退款完成
     */
    public function changeToRefunded()
    {
        $this->update([
            'trade_status'=>6,
            'refund_status'=>2,
            'refund_at'=>time(),
            'closed'=>1,
            'closed_at'=>time(),
            'updated_at'=>time()
        ]);

        if ($trade = $this->trade)
        {
            $trade->update([
                'trade_status'=>6,
                'updated_at'=>time()
            ]);
        }
    }

    /**
     * 更改为关闭状态
     */
    public function changeToClosed()
    {
        $this->update([
            'trade_status'=>6,
            'closed'=>1,
            'closed_at'=>time(),
            'updated_at'=>time()
        ]);

        if ($trade = $this->trade)
        {
            $trade->update([
                'trade_status'=>6,
                'updated_at'=>time()
            ]);
        }
    }

    /**
     * @param $query
     * @param int $status
     * @return mixed
     */
    public function scopeStatus($query, $status = 1)
    {
        return $query->where('trade_status', $status);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function actions()
    {
        return $this->hasMany(OrderAction::class, 'order_id', 'order_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function closeReason()
    {
        return $this->hasOne(OrderClosed::class, 'order_id', 'order_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function refund()
    {
        return $this->hasOne(OrderRefund::class, 'order_id', 'order_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function shipping()
    {
        return $this->hasOne(OrderShipping::class, 'order_id', 'order_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function trade()
    {
        return $this->hasOne(Trade::class, 'trade_no', 'trade_no');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function items()
    {
        return $this->hasMany(OrderItem::class, 'order_id', 'order_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function buyer()
    {
        return $this->belongsTo(User::class, 'buyer_uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function seller()
    {
        return $this->belongsTo(User::class, 'seller_uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function invoice()
    {
        return $this->hasOne(OrderInvoice::class, 'order_id', 'order_id');
    }
}
